from flask import Blueprint,render_template,redirect
from .wxapi import _weixin
import json

weixin = Blueprint('weixin',__name__,template_folder='templates')
wx = _weixin()

@weixin.route('/')
def index():
    if wx.check_login():
        data = wx.getMyInfo()
        return render_template('weixin/info.html',data=data)
    else:
        return redirect('/weixin/login')

@weixin.route('/login')
def login():
    return render_template('weixin/login.html')

@weixin.route('/logout')
def logout():
    wx.logout()
    return redirect('/weixin/login')


@weixin.route('/getQrcodeurl')
def getQrcodeurl():
    if not wx.islogin:
        Qrpic = wx.getQr()
        jsdata = {'Qrpic':Qrpic,'uuid':wx.uuid}
        return json.dumps(jsdata)

@weixin.route('/tologin',methods=['POST'])
def tologin():
    js = wx.tologin()
    return js